﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProcessFile;

namespace ICD2AD14
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnImoport_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Stackup Planner file (.stk)|*.stk|All Files (*.*)|*.*";
            if (dialog.ShowDialog()==DialogResult.OK)
            {
                txtIcd.Text = dialog.FileName;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "Ad14(*.stk)|*.stk|All Files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string content = ProcessFile.ConvertAD144Stk.ConvertFilesAd144Stk(txtAd14.Text);
                System.IO.File.WriteAllText(saveFileDialog1.FileName, content);
                MessageBox.Show("Success");

            } 
        }

        private void btnExportToAd14_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "Ad14(*.stackup)|*.stackup|All Files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string content = ProcessFile.ConvertStk4AD14.ConvertFilestk4Ad14(txtIcd.Text);
                System.IO.File.WriteAllText(saveFileDialog1.FileName,content);
                MessageBox.Show("Success");
               
            } 
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Stackup Planner file (.stackup)|*.stackup|All Files (*.*)|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtAd14.Text = dialog.FileName;
            }
        }

        private string[] files;
        private void btnOpenMultifileStk_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Stackup Planner file (.stk)|*.stk|All Files (*.*)|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
              //  listFile.Items.Clear();
                listFile.Items.AddRange(dialog.FileNames);
                files = dialog.FileNames;
            }
        }

        private void btnExportAD14Multi_Click(object sender, EventArgs e)
        {
            if (listFile.Items.Count==0)
            {
                MessageBox.Show("ICD Stackup Planner file not empty");
                return;
            }
            if (txtAd14Stage2.Text.Length== 0)
            {
                MessageBox.Show("AD14 file template not empty");
                txtAd14Stage2.Focus();
                return;
            }
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "Ad14(*.stackup)|*.stackup|All Files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            List<string>files=new List<string>();
            foreach (var item in listFile.Items)
            {
                files.Add(item.ToString());
            }
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string content = ProcessFile.ConvertStk4AD14.ConvertFilestk4Ad14(files.ToArray(),txtAd14Stage2.Text);
                System.IO.File.WriteAllText(saveFileDialog1.FileName, content);
                MessageBox.Show("Success");
            } 
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            listFile.Items.RemoveAt(listFile.SelectedIndex);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void btnOpenFIleAD14Stage2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Stackup Planner file (.stackup)|*.stackup|All Files (*.*)|*.*";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                txtAd14Stage2.Text = dialog.FileName;
            }
        }

        private void btnExportStkStage2_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            //saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            //saveFileDialog1.Filter = "Ad14(*.stk)|*.stk|All Files (*.*)|*.*";
            //saveFileDialog1.FilterIndex = 1;
            FolderBrowserDialog fbd = new FolderBrowserDialog();
          
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                List<SubStackItem> list= new List<SubStackItem>();
                string path = fbd.SelectedPath;
                string[] content = ProcessFile.ConvertAD144Stk.ConvertFilesAd144StkStage2(txtAd14Stage2.Text, ref list);
                for (int i = 0; i < content.Length; i++)
                {
                    System.IO.File.WriteAllText(path + "//" + list[i].SubName + ".stk", content[i]);
                }
               
                   
         
                
                MessageBox.Show("Success");

            } 
        }

        
    }
}
